package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiFactory;
import com.hilaver.dzmis.codetable.CtFactoryType;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.service.IEntityService;
import com.hilaver.dzmis.util.SimpleObj2XML;

public class BiFactoryServiceImpl extends AbstractBaseService implements
		IEntityService {
	
	public final static String COLUMN_FACTORY_TYPE = "ctFactoryType";

	public final static String COLUMN_HAS_LOOM = "hasLoom";
	
	public final static String COLUMN_HAS_ELECTRONIC = "hasElectronic";

	public final static String COLUMN_HAS_INTARSIA = "hasIntarsia";
	
	public final static String COLUMN_HAS_KNITTING = "hasKnitting";

	private CtServiceImpl ctService;
	
	public BiFactoryServiceImpl(){
		this.ctService = new CtServiceImpl();
	}
	
	@Override
	public String edit(Object obj) throws Exception {
		BiFactory factory = (BiFactory)obj;
		String operation = null;
		if(factory.getId() != null){
			operation = "02";
		}else{
			operation = "01";
		}
		this.dao.saveOrUpdate(obj);
		super.editSysMbox(obj, operation);
		return "{success: true}";
	}
	
	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(BiFactory.class.getName(), id);
		return super.delete(BiFactory.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		return super.get(BiFactory.class.getName(), id);
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		return super.getAllPagination(BiFactory.class.getName(), page, sort, order);
	}
	
	public String getAll(String sort, String order) throws Exception{
		List<BiFactory> objList = this.dao.getAll(BiFactory.class.getName(), sort, order);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (BiFactory obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			
			xmlSB.append("<" + COLUMN_FACTORY_TYPE + ">" );
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtFactoryType.class.getName(), obj.getCtFactoryType(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_FACTORY_TYPE + ">" );
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(objList.size());
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAllPagination(int page, String sort, String order, String[] filters)
			throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<BiFactory> objList = this.dao.getAllPagination(BiFactory.class.getName(), offset,
				Constants.PAGE_SIZE, sort, order, filters);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (BiFactory obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<" + COLUMN_FACTORY_TYPE + ">" );
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtFactoryType.class.getName(), obj.getCtFactoryType(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_FACTORY_TYPE + ">" );
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(BiFactory.class.getName(), filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

}
